/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package twi_project;

/**
 * 
 * @author Soeren
 */
public class Utils {

	public static byte[] stringToByte(String in) {
		return in.getBytes();
	}

	public static byte[] intToByte(int in) {
		byte[] data = new byte[4];
		for (int i = 0; i < 4; ++i) {
			int shift = i << 3; // i * 8
			data[3 - i] = (byte) ((in & (0xff << shift)) >>> shift);
		}
		{
			byte[] out = { data[3], data[2], data[1], data[0] };
			return out;
		}
	}

	public static byte[] floatToByte(float f) {
		return intToByte(Float.floatToIntBits(f));
	}

	public static float byteToFloat(byte[] b) {
		return Float.intBitsToFloat(byteToInt(b));
	}

	public static int byteToInt(byte[] in) {
		int value = in[3] << 24 | in[2] << 16 | in[1] << 8 | in[0];
		return value;
	}

	public static String byteToString(byte[] b) {
		String out = "";
		for (int i = 0; i < b.length; i++) {
			out += Character.valueOf((char) b[i]);
		}
		return out;
	}
}
